/*
 *  Session.cpp
 *  gre
 *
 *  Created by Kutlak Roman on 15/04/2012.
 *  Copyright 2012 Kutlak Roman. All rights reserved.
 *
 */

#include <iostream>

#include "Session.h"

namespace kutlak {
namespace roman {
namespace gre {
    
    // class implemented
    /////////////////////////////// PUBLIC   ///////////////////////////////////
    
#pragma mark -
#pragma mark lifecycle
    Session::Session(pKnowledgeBase kb, pOntology onto) :
    mVoc(Voc::Instance()), mpKb(kb), mpOnto(onto) 
    {
        if (!mpKb)
            mpKb = std::make_shared<class KnowledgeBase>();
        if (!mpOnto)
            mpOnto = std::make_shared<class Ontology>();
    }
    
    Session::Session(const Session& from) = default;
    
    Session& Session::operator=(Session from) noexcept(true)
    {
        swap(*this, from);
        return *this;
    }
    
    void swap(Session& lhs, Session& rhs) noexcept(true)
    {
        swap(lhs.mpKb, rhs.mpKb);
        swap(lhs.mpOnto, rhs.mpOnto);
    }
    
    Session::~Session() noexcept(true) = default;
    
#pragma mark -
#pragma mark operators
    
    
#pragma mark -
#pragma mark I/O

     ostream& operator<<(ostream& out, const Session& obj)
     {
         obj.Serialise(out);
         return out;
     }
     
     istream& operator>>(istream& input, Session& obj)
     {
         try
         {
             Session tmp;
             tmp.Deserialise(input);
             swap(obj, tmp);
         }
         catch (DeserialisationException& de)
         {
             std::cerr << de.what() << '\n';
             std::cerr << '\t' << de.data() << '\n';
         }
         return input;
     }
    
    void Session::Serialise(std::ostream& out) const
    {
        out << mVoc << std::endl;
        out << *mpKb << std::endl;
        out << *mpOnto << std::endl;
    }
    
    void Session::Deserialise(std::istream& in)
    {
        Voc v;
        in >> v;
        in >> *mpKb;
        in >> *mpOnto;
        
        swap(v, mVoc);
    }

} // gre
} // roman
} // kutlak
